package com.cy.store.controller;


import com.cy.store.entity.Cart;
import com.cy.store.entity.Product;
import com.cy.store.service.ICartService;
import com.cy.store.service.IProductService;
import com.cy.store.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import util.JsonResult;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("carts")
public class CartController extends BaseController{
    @Autowired
    private ICartService iCartService;

    @RequestMapping("add_to_cart")
    public JsonResult<Void> addToCart(HttpSession session,Integer pid,Integer amount){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        iCartService.addToCart(uid,pid,username,amount);
        return new JsonResult<>(Ok);

    }

    @RequestMapping({"", "/"})
    public JsonResult<List<CartVo>> getVoByUid(HttpSession session){
        Integer uid = getUidFromSession(session);
        List<CartVo> list = iCartService.getVoByUid(uid);
        return new JsonResult<>(Ok,list);
    }

    @RequestMapping("{cid}/num/add")
    public JsonResult<Integer> addNum(@PathVariable("cid") Integer cid,HttpSession session){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        Integer data = iCartService.addNum(cid,uid,username);
        return new JsonResult<>(Ok,data);

    }
    @RequestMapping("{cid}/num/reduce")
    public JsonResult<Integer> reduceNum(@PathVariable("cid") Integer cid,HttpSession session){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        Integer data = iCartService.reduceNum(cid,uid,username);

        return new JsonResult<>(Ok,data);

    }

    @RequestMapping("list")
    public JsonResult<List<CartVo>> getVoByCid(HttpSession session,Integer[] cids){
        Integer uid = getUidFromSession(session);
        List<CartVo> list = iCartService.getVoByCids(cids,uid);
        return new JsonResult<>(Ok,list);
    }



}
